// pages/cashRedpag/index.js
const app = getApp()
var cashLow = 100;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    canse: false,
    canUse: wx.canIUse('button.open-type.getUserInfo'),
    disabled: false,
    userBasic:{},
    amount:0,
    cashLow :100,
  },
  getStorage: function () {
    var that = this;
    try {
      const value = wx.getStorageSync('userInfo')
      console.log('读取缓存', value)
      if (value) {
        that.getUserBasicInfo();
      } else {
        that.getUserInfo();
      }
    } catch (e) {
      //this.getUserInfo();
      app.prompt('登陆失败' + e);
    }
  },
  /**
* 用户授权
*/
  bindGetUserInfo: function (e) {
    var that = this;
    if (e.detail.userInfo) {
      //用户按了允许授权按钮
      that.getUserInfo();
    } else {
      app.prompt('登陆失败');
    }
  },
  getUserInfo: function () {
    var that = this;
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          console.log('已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框')
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          app.getUserInfo(that);
        } else {
          that.setData({
            canse: true
          })
        }
      }
    })
  },
  
//获取个人基本信息
  getUserBasicInfo:function(){
    var that = this;
    var data = [];
    var url = 'We_Community/getBrowsePersonalUserBasic'
    var calkdata = app.netWorkRequest(that, data, url, 'userBasic')
  },

  testAmount:function(e){
    var that = this;
    var amount = e.detail.value;
    var d = that.data;
    var quota = d.userBasic['Smallchange'];
    var casamount = d.userBasic['Cash_amount'];
    var cashLow = d.cashLow;
    if (amount > quota)
    amount = quota;
    if (amount * 1 > (cashLow * 1 - casamount * 1) || amount * 1 > cashLow * 1)
    return app.promptModal('提现金额超过限制');
    that.setData({
      amount: amount
    })
  },
  //零钱提现
  sendCashRedpag:function(e){
    var that = this;
    var dt = e.detail.value;
    console.log('form发生了submit事件，携带数据为：', dt)
    var amount = dt.amount;
    var d = that.data;
    var quota = d.userBasic['Smallchange'];
    var cashLow = d.cashLow;
    console.log(amount,quota)
    if (!amount || amount == '' || amount < 1 ) 
      return app.promptModal('请输入提现金额');
    // if (amount < 1 || amount < cashLow)
    //   return app.promptModal('提现金额不能低于' + cashLow+'元');
    if (amount > quota) 
      return app.promptModal('零钱余额不足');
    that.setData({
      disabled: true,
    })
    var data = [];
    data['amt'] = amount;
    var url = 'We_Redenvelopes/getCashRedenvelopePayWithdrawal'
    var calkdata = app.netWorkRequest(that, data, url, 'cashBlanc')
  },

  initData:function(){
    this.setData({
      canse: false,
      disabled: false,
      amount: 0,
      cashLow: 100,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getStorage();
    this.initData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onShow();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})